package com.klangner.move.behavior
{
  import com.klangner.geom.Vector2D;
  
  import flash.geom.Rectangle;

  /**
  * Apply force to object when outside rectangle.
  */
  public class RectangleBehavior implements IBehavior
  {
    private var _rectangle:Rectangle;
    
    /**
    * Constructor
    * @param point flee from this point
    */
    public function RectangleBehavior(rect:Rectangle){
      _rectangle = rect;
    }
    
    /**
    * Update position.
    */
    public function updateForce(obj:ISteeredObject):void{
      var velocity:Vector2D = new Vector2D();
      
      if(obj.position.x < _rectangle.left){

        velocity.x = _rectangle.left - obj.position.x;

      }else if(obj.position.x > _rectangle.right){

        velocity.x = _rectangle.right - obj.position.x;
        
      }else if(obj.position.y < _rectangle.top){
        
        velocity.y = _rectangle.top - obj.position.y;
        
      }else if(obj.position.y > _rectangle.bottom){
        
        velocity.y = _rectangle.bottom - obj.position.y;
      }
      else{
        return;
      }

      velocity.normalize();
      obj.setDesiredVelocity(velocity);
    }

  }
}